USE Liberty
GO
-- ------------------------------------------------------------------------------------------------
-- CaseEvents create table script
-- ------------------------------------------------------------------------------------------------
SET NOCOUNT OFF
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
SET NUMERIC_ROUNDABORT OFF
GO

PRINT 'Creating CaseEvents table...'
GO

IF OBJECT_ID( 'CaseEvents' ) IS NULL
BEGIN
    CREATE TABLE dbo.CaseEvents 
	(OID				bigint		IDENTITY(1, 1)	NOT NULL
	,Version			rowversion
	,CaseID				bigint						NOT NULL
	,ActivityID			bigint						NOT NULL 
	,UserID				bigint						NOT NULL 
	,EventOccurred		datetime					NOT NULL
		CONSTRAINT DFLT_CaseEvents_EventTime DEFAULT GETDATE()
	,EventNotes			varchar(MAX)					NULL

	,CONSTRAINT PK_CaseEvents PRIMARY KEY CLUSTERED (OID)

	,CONSTRAINT FK_CaseEvents_Cases FOREIGN KEY (CaseID)
		REFERENCES DBO.Cases(OID)

	,CONSTRAINT FK_CaseEvents_TypeOfActivity FOREIGN KEY (ActivityID) 
		REFERENCES DBO.TypesOfActivity(OID)

	,CONSTRAINT FK_CaseEvents_Users FOREIGN KEY (UserID)
		REFERENCES DBO.Users(OID)

	)
	PRINT 'CaseEvents table has been created...'
END
ELSE
BEGIN
	PRINT 'CaseEvents table already exists'
END
GO

